package mobi.workflowengine.core;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import android.content.Context;
import android.content.Intent;

public class WorkflowTaskManager extends WorkflowTask
{
	public WorkflowTaskManager(WorkflowTaskEndCallback onEnd)
	{
		super(onEnd);
	}
	
	private static File WFLogFile = null;
	private static boolean WFLogFileEnabled = true;
	
	public static String LogFileName = null;
	static boolean ErrorCreatingOrWritingToLogFile = false;
	
	public void appendLog(String text)
	{   
		if(LogFileName==null || ErrorCreatingOrWritingToLogFile)
			return;
		
		if(WFLogFileEnabled)
		{
			if(WFLogFile == null)
			{
				//WFLogFile = new File("sdcard/sharkband_wf_log.file");
				WFLogFile = new File(LogFileName);
			   if (!WFLogFile.exists())
			   {
			      try
			      {
			    	  WFLogFile.createNewFile();
			      } 
			      catch (IOException e)
			      {
			    	  ErrorCreatingOrWritingToLogFile = true;
			         e.printStackTrace();
			      }
			   }
			}
		   try
		   {
		      //BufferedWriter for performance, true to set append to file flag
		      BufferedWriter buf = new BufferedWriter(new FileWriter(WFLogFile, true)); 
		      buf.append(text);
		      buf.newLine();
		      buf.flush();
		      buf.close();
		   }
		   catch (IOException e)
		   {
			   ErrorCreatingOrWritingToLogFile = true;
		      e.printStackTrace();
		   }
		}
	}

	public static WorkflowTask MajorWorkfFlow = null;
	public static WorkflowTaskManager MainWorkflow = new WorkflowTaskManager(null);

	public void jumpStartNewWorkflow(WorkflowTask newWFA) {
		if(MajorWorkfFlow!=null) MajorWorkfFlow.cancel();
		
		MajorWorkfFlow = newWFA;
		MajorWorkfFlow.start(MainWorkflow);
	}
	
	public boolean startNewWorkflow(WorkflowTask newWFA)
	{
		if(MajorWorkfFlow==null || MajorWorkfFlow.completedSuccessfully())
		{
			MajorWorkfFlow = newWFA;
			MajorWorkfFlow.start(MainWorkflow);
			return true;
		}
		else
		{
			this.notifyWorkflowMessage(WorkflowStatus.FailedToStartAsATaskIsRunning.getValue());
			return false;
		}
	}
	
	public static void CloseWorkflowMessage(Context context,final int secondsAfter)
	{
		final long startTime = System.currentTimeMillis();
		
		Thread t = new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				while(true)
				{
					if(((System.currentTimeMillis()-startTime)/1000)<secondsAfter)
					{
						try{
						Thread.currentThread().wait(500);
						}
						catch(Exception e)
						{
							//Never mind
						}
					}
					else
						break;
				}
				
				WorkflowMessageActivity.closeIt();

			}
		} );
		
		t.start();
		
	}
	
	public static void ShowWorkflowMessage(Context context,String message)
	{
		WorkflowMessageActivity.clearMessage();
		WorkflowMessageActivity.addMessage(message);
		if(!WorkflowMessageActivity.isRunning())
		{
			Intent intent = new Intent(context , WorkflowMessageActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			context.startActivity(intent);
		}
		else
		{
			WorkflowMessageActivity.updateMessage();
		}
	}
	
	public static void AddToWorkflowMessage(Context context,String message)
	{
		WorkflowMessageActivity.addMessage(message);
		if(!WorkflowMessageActivity.isRunning())
		{
			Intent intent = new Intent(context, WorkflowMessageActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	
			context.startActivity(intent);
		}
		else
		{
			WorkflowMessageActivity.updateMessage();
		}
	}
	
	public static void CloseWorkflowMessage()
	{
		WorkflowMessageActivity.closeIt();
	}



	@Override
	public void cancel() {
	}


	@Override
	public void onNotification(int workflowCode) {

	}

	@Override
	public void start(WorkflowTask pParentWorkflow) {		
	}

	
}